
##################################################################################
######## Replication: Border Barriers and Local Armed Uprisings ##################
######## Security Studies, 2025 ##################################################
######## Md Muhibbur Rahman ######################################################
######## Cornell University ######################################################
######## March, 2025 #############################################################
##################################################################################

### Figure 2: Distribution of barriers at state boundaries


# Load libraries
library(tidyverse)
library(sf)
library(rnaturalearth)
library(countrycode)
library(ggrepel)
library(readstata13)

# Load data
data <- read.dta13("ss_replication_data.dta")

# Load world map
world <- ne_countries(scale = "small", returnclass = "sf")

# Convert country codes
data_with_iso <- data %>%
  mutate(Iso3 = countrycode::countrycode(
    sourcevar = ccode,  
    origin = "cown",  
    destination = "iso3c"
  )) %>%
  filter(!is.na(Iso3))

# Join with world map
mydata <- world %>%
  select(geometry, name, iso_a3) %>%
  left_join(data_with_iso, by=c("iso_a3"="Iso3")) %>%
  filter(bw_dummy == TRUE)

# Create map
map_plot <- world %>%
  filter(admin != "Antarctica") %>%
  st_transform(crs = "+proj=robin") %>%
  ggplot() +
  geom_sf(color = "orangered", fill = "white") + # Ensure the map has a white fill
  geom_sf(data = mydata, aes(fill = factor(bw_count))) +
  theme_void() +
  theme(
    legend.position = "bottom",
    panel.background = element_rect(fill = "white", color = NA), # Set white background
    plot.background = element_rect(fill = "white", color = NA)   # Set white plot background
  ) +
  scale_fill_manual(values = c("lightblue", "royalblue", "mediumblue", "navy", "brown", "red", "orange")) +
  labs(fill = "Number of Barriers at the Border\n(Light Grey Color: No Barrier)")

   
# Save as TIFF 
   ggsave("border_barriers_map.tiff", plot = map_plot, dpi = 300, width = 10, height = 6, units = "in", device = "tiff", bg = "white")
 
